﻿using System.Collections.Generic;
using System.Configuration;
using System.Data.Linq;
using System.Linq;
using DAL;
using DAL.Model;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace BLL
{
    public class ExistingAnswer
    {
        public Question Question { get; set; }
        public tableanswers Answer { get; set; }
    }

    public class QuestionManager
    {

        public Question GetAquestion(int quiestonID)
        {
            using (var db = new dbRazorQuizDataContext())
            {
                var options = new DataLoadOptions();
                options.LoadWith<Question>(d => d.Options);
                db.LoadOptions = options;
                return db.Questions.FirstOrDefault(d => d.QuestionID == quiestonID);
            }
        }

        public Question GetNextQuestion(int id)
        {
            using (var db = new dbRazorQuizDataContext())
            {
                var next = db.Questions.ToList()
                 .SkipWhile(item => item.QuestionID != id)
                 .Skip(1)
                 .FirstOrDefault();

                return next;
            }
        }

        public bool IsValid(int questionID, int answerID)
        {
            using (var db = new dbRazorQuizDataContext())
            {
                var next = db.Answers.FirstOrDefault(d => d.QuestionID == questionID);

                if (next == null)
                    return false;

                return next.OptionID == answerID;
            }
        }

        public void SubmitAnswer(int questionID, int selectedAnswerID, string userID)
        {
            var answer = new AnswerToQue
                             {
                                 QuestionID = questionID,
                                 QuizID = 1,
                                 OptionID = selectedAnswerID,
                                 UserID = userID
                             };

            var que = GetQue();
            var messageData = AnswerToQue.Serialize(answer);
            var msg = messageData;
            que.AddMessage(new CloudQueueMessage(msg));
        }

        public static CloudQueue GetQue()
        {
            CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => configSetter(ConfigurationManager.AppSettings[configName]));
            var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            var queueClient = storageAccount.CreateCloudQueueClient();
            var queue = queueClient.GetQueueReference(AnswersStorage.QUE_NAME);
            queue.CreateIfNotExist();
            return queue;
        }

        public List<ExistingAnswer> GetAnswersForUser(string userID)
        {
            var answers = AnswersStorage.GetAllAnswers(userID);

            var result = new List<ExistingAnswer>();
            foreach (var answer in answers)
            {
                var question = GetAquestion(answer.questionid);
                var q = new ExistingAnswer()
                            {
                                Question = question,
                                Answer = answer
                            };
                result.Add(q);
            }

            return result;
        }
    }
}
